#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
int arr[1000010] = { 0 };
int judge(int n)
{
	int count = 0;
	while (n > 0)
	{
		if (n % 10 != 0)
		{
			count++;
			if (count > 1) return 0;
		}
		n /= 10;
	}
	return 1;
}
int main()
{
	int t = 0;
	int n = 0;
	for (int i = 1; i <= 999999; i++)
	{
		arr[i] = arr[i - 1];
		if (judge(i))
		{
			arr[i] ++;
		}
	}
	cin >> t;
	while (t--)
	{
		cin >> n;
		cout << arr[n]<<endl;

	}
	return 0;
}